//implementing Hash table by open addressing
//Devika Sondhi(2010028)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
struct info
{
  int key;
  char user[20];
  char psswrd[20];
};
struct info hash[1000];

printf("\t\tWelcome\nYou may refer to the data.txt file to verify data\n");

printf("Enter hash table size(greater than 30:");     // varying the value of alpha(load factor)
int size;
scanf("%d",&size);

FILE *data;
char *str=malloc(1000);
int i=0,j=0,k,sum=0,x=0;
data=fopen("data.txt","r");
if (data==NULL)
        {
        puts("Cannot open source file");
        exit(1);        
        }
else
        {
        
        {
                while(fscanf(data,"%s %s",hash[x].user,hash[x].psswrd)!=EOF)
                {
                        //printf("%s \t %s \n",hash[x].user,hash[x].psswrd);
                        for(i=0;i<strlen(hash[x].user);i++)             
                                {str[j]=hash[x].user[i];        //usernames are stored in str   
                                j++;}
                        str[j]=' ';j++;x++;             
                }
                str[j]='.';
        }
        }
i=0,x=0;
int *c;
c=malloc(10000);
for(k=0;k<=j;k++)
{
c[i]=str[k];                           //c stores ascii values of all username characters
//printf("%c",str[k]);
i++;
}

//for(x=0;x<i;x++)
//printf("%d  ",c[x]);
x=0;i=0;k=0;j=0;   
while(1)
{
        if(c[j]==32)
        {
        hash[x].key=sum%size;            //hash[].key stores sum of product of pos*asciival of username--hash function implementation
        sum=0;
        k++;j++;x++;    
        i=0;
        }
        else if(c[j]==46)
        break;
        else
        {
        sum=sum+i*c[j];
        j++;
        i++;
        }
        
}
/*for(j=0;j<k;j++)             //displays data stored
{
  printf("\n%d ",hash[j].key);
  printf(" %s",hash[j].user);
  printf(" %s",hash[j].psswrd);
}*/

fclose(data);
struct account
{
char usr[20];
char psw[20];
};
i=0;
struct account table[size];

//RANDOM HASH FUNCTION

while(i<size)
{
  if(table[hash[i].key].usr[0]==NULL)
  {
  strcpy(table[hash[i].key].usr,hash[i].user);
  strcpy(table[hash[i].key].psw,hash[i].psswrd);
  i++;
  }
  else 
  (hash[i].key++)%size;
}
/*for(j=0;j<size;j++)
{
printf("%s %s\n",table[j].usr,table[j].psw);
}*/
j=0;
int count=0;
char name[20];
char pass[20];
printf("\nEnter username:");
scanf("%s",name);
printf("Enter password:");
scanf("%s",pass);

while(j<size)
{
if(strcmp(name,table[j].usr)==0 && strcmp(pass,table[j].psw)==0)
        {
        printf("\nPassword Matched\n");break;
        
        }

else 
{
j++;
}
}
if(j>=size)
{
printf("Incorrect entry\n");
j=0;
while(count<2)
{
  printf("\nEnter username:");
  scanf("%s",name);
  printf("Enter password:");
  scanf("%s",pass);
  while(j<size)
  {
  if(strcmp(name,table[j].usr)==0 && strcmp(pass,table[j].psw)==0)
     {
     printf("\nPassword Matched\n");break;
     }
  else 
    {
    j++;
    }
  }

  if(j>=size)
  printf("Incorrect entry\n");
  else exit(1);
  j=0;
  count++;
}
}
}

